* This do-file assembles survey outcomes related to food storage

tempfile pulse_stock

forvalues y = 1/3 {
	if `y'==1 {	
	*** Kharif 2017: FARM SURVEY HAS TIME LEFT ONLY
		tempfile stock17	
		* Farm responses
		use hhid cropcode stock using "$k1/Farm/areas_inputs_outputs.dta", clear
			duplicates drop
			rename stock Mtime_end
			replace Mt = Mt+12		// Subtracted off later
		save `stock17', replace
		
		* Food responses
		use hhid crop_code instock_grams time_end time_instock using "$k1/Food/storage.dta", clear
			recode crop_code (2=3) (3=2)
			rename crop_code cropcode
			gen Finstock_kg = instock_grams/1000
			drop instock_grams
			rename time_* Ftime_*
		merge 1:1 hhid cropcode using `stock17', nogen
	}
	
	else if `y'==2 {
	*** Kharif 2018 (post Year 1) results
		use hhid crop_code time_end time_instock instock_kg using "$k2/Farm/storage.dta", clear
			rename (time_end time_instock instock_kg) (Mtime_end Mtime_instock Minstock_kg)
		merge 1:1 hhid crop_code using "$k2/Food/storage.dta", keepusing(Ftime_end Ftime_instock Finstock_kg) nogen
			rename crop_code cropcode
	}
	
	else if `y'==3 {
	*** Kharif 2019 (post Year 2) results
		tempfile stock19
		* Farm responses (Kharif 2019 survey only)
		use hhid past_cropcode *month* rstock*kg using "$k3/Farm/stocks.dta", clear
			rename past_cropcode cropcode
			
			* Months left in stock
			gen byte Mtime_end = month_end_rstock
			
			* Total months instock (assume harvest in month 3 if not specified)
			gen month_runout = month_end_rstock+12
			recode month_out_rstock (10=-2) (11=-1) (12=0) (101=13) (110=10) (111=11) (112=12) (-999=1) 
			replace month_runout = month_out_rstock if missing(month_runout) & !missing(month_out_rstock)
			replace past_month_harvested = 3 if missing(past_month_harvested) & !missing(month_runout)
			gen byte Mtime_instock = max(0, month_runout - past_month_harvested)
			
			* Kgs in stock
			replace rstock_proc_kg = rstock_proc_kg*1.3
			egen Minstock_kgs = rowtotal(rstock*), missing
			/*foreach p in proc unproc {
				gen kg_`p' = qty_instock_`p' * (unit_instock_`p'==1) ///
					+ qty_instock_`p'*100 * (unit_instock_`p'==2) ///
					+ qty_instock_`p'/1000 * (unit_instock_`p'==3) if !missing(qty_instock_`p')
			}*/
			
			keep hhid cropcode M*
		save `stock19', replace

		* Food responses
		use hhid crop_code qty_instock_* unit_instock_* month_end using "$k3/Food/stocks.dta"
			rename crop_code cropcode
			
			* Months left instock
			recode month_end (-999=25)
			gen byte Ftime_end = month_end - 24
			
			* Kgs in stock
			foreach p in proc unproc {
				gen kg_`p' = qty_instock_`p' * (unit_instock_`p'==1) ///
					+ qty_instock_`p'*100 * (unit_instock_`p'==2) ///
					+ qty_instock_`p'/1000 * (unit_instock_`p'==3) if !missing(qty_instock_`p')
			}
			replace kg_proc = kg_proc*1.3
			egen Finstock_kg = rowtotal(kg_proc kg_unproc), missing
			
			keep hhid cropcode F*
		merge 1:1 hhid cropcode using `stock19', nogen
	}

	keep if inrange(cropcode,1,10)
		// Kharif 2019 only has farm past runout, so don't use food for 2018 either
	if `y'==1 gen byte pulse_time = Ftime_instock
	if `y'> 1 gen byte pulse_time = Mtime_instock 
	egen stock_kgs  = rowmax(*instock_kg)
	egen byte stock_time = rowmax(Mtime_end Ftime_end)
		replace stock_time = stock_time-12

	* Combine into sensible categories
	recode cropcode (5=4) (7=5) (4 6 8 9 10=7)
	collapse (max) pulse_time stock_time (sum) stock_kgs, by(hhid cropcode)
	reshape wide stock_* pulse_time, i(hhid) j(cropcode) 
	rename (*1 *2 *3 *4 *5 *7) (*_pp *_ll *_gp *_gg *_fv *_ot)
	
	* Winsorize stocks before adding in 0s
	foreach c in pp ll gp gg fv ot {
		winsor stock_kgs_`c', gen(stock_kw5_`c') highonly p(.05)
	}

	* Fill in zero stocks
	if `y'==1 {
		merge 1:1 hhid using "$admin_data/survey_list.dta", nogen
		drop village
		recode stock_* pulse_time_* (-999 . = 0)
	}
	else if `y'>1 {
		merge 1:1 hhid using "$out_data/survey_drops.dta", keepusing(drop_k`y') nogen
		recode stock_* pulse_time_* (-999 . = 0) if drop_k`y'==0
		drop drop_k`y'
	}
	
	gen byte year = `y'

	if `y'>1 append using `pulse_stock'
save `pulse_stock', replace
}

order hhid year pulse_time_* stock_time_* stock_kgs_* stock_kw5_*
save "$out_data/outcome_stocks.dta", replace
